from project import *
from person import *
from keyexpression import *
from Tkinter import *
from database import *
from client import *
from Tkinter import *
from client import *
from tkSimpleDialog import *

class ProjectMembersFrame(Frame):
    """
    Represents the frame that provides a user interface to access project information.
    """
    
    def __init__(self, parentControl, project, serverProxy ):
        Frame.__init__(self, parentControl)
        self.parentControl = parentControl
        self.project = project
        self.serverProxy = serverProxy

        
        label = Label(parentControl, text="Nom du projet:")
        label.pack()
        
        self.Label = Label(parentControl, width=20, height=1)
        label["text"] = "#- " +str(project.id)
        self.Label.pack()
        
        label = Label(parentControl, text="Membres: ")
        label.pack()
        
        self.allMembers = Text(parentControl, width=40, height=15)
        self.allMembers.pack()
        
        button = Button(parentControl, text="Sauvegarder", command=self.save)
        button.pack()
        
    def save(self):
        memberNamesString = self.allMembers.get(1.0, END)
        memberNames = memberNamesString.split("\n")
        
        for memberName in memberNames:
            if memberName == "":
                continue
            
            memberNameParts = memberName.split(" ")
            if len(memberNameParts) < 2:
                continue
            
            memberFirstName = memberNameParts[0]
            memberLastName = memberNameParts[1]
            
            person = self.serverProxy.createPerson(memberFirstName, memberLastName)
            self.serverProxy.addProjectMember(person.id, project.id)
            
    
if __name__ == "__main__":
    root = Tk()
    client = ServerProxy()
    project = client.createProject("Foo", "Bar", "Frob")
    frame = ProjectMembersFrame(root, project, client)
    root.mainloop()